import { MD } from "./MD/GMDBase/MD"
import UTimeLine from "./MD/GMDCocosBase/UTimeLine";
import SimulationProgress from "./MD/GMDBase/SimulationProgress";
import BaseEventDispatcher from "./MD/GMDBase/BaseEventDispatcher";
import Login from "./MD/GMDWeakNetGameBase/Login";
import { platform } from "./MD/GMDBase/platform";
import WndManage from "./MD/GMDCocosBase/Wnd/WndManage";
import SafeTime from "./MD/GMDNet/SafeTime";
import { AnalyticsMgr } from "./MD/GMDAnalytics/AnalyticsMgr";
import { WndDebug } from "./wnd/WndDebug";
import { WndLayers } from "./ui/WndLayers";

//游戏初始化组件
const { ccclass, property } = cc._decorator;

@ccclass
export default class GameInit extends cc.Component {
 
    start() {

        if(platform.Start)
        {
            platform.Start(()=>{
                this.doStart()
            })
        }else
        {
            this.doStart()
        }

       
    }

    doStart()
    { 
        //初始化窗体管理器单例
        MD.WndManage = this.getComponent(WndManage)
      
        /*
        //这里可以加载游戏Loading界面 
        //以及Loading界面可能需要用到的界面
        MD.WndManage.LoadWnds(
            [
                "Prefabs/WndLoading",//Loading界面
                "Prefabs/WndBanner",//原生Banner
                "Prefabs/WndInterstitial"//原生插屏
            ],
            WndLayers.Loading
        )
        //动态装载窗体
        MD.WndManage.LoadWnds(
            [
                "Prefabs/WndMainBg",//背景图
            ],
            WndLayers.Base
        )*/


        //等待Loading窗体装载完成
        MD.WndManage.WaitLoadDone(() => {
            

            //初始化内核，并等待本地数据可用
            platform.InitMD(
                () => {

                    //框架层档案就绪事件通知
                    if (Login.Singletion) {
                        Login.Singletion.WaitCanUseLocalData(
                            {
                                showLoading: false,
                                success: () => {
                                    BaseEventDispatcher.OnArchiveReady()
                                }
                            }
                        )
                    } else
                        BaseEventDispatcher.OnArchiveReady()

            
                    this.LoadWnds()
                }
            )



        })
    }

    //装载窗体资源
    LoadWnds() {

        //这里可以加载游戏逻辑需要用到的界面
         
        /*
        //弹出窗体
        MD.WndManage.LoadWnds(
            [
                "Prefabs/WndWait"
            ],
            WndLayers.Loading
        )
        */

        //底层窗体
        MD.WndManage.LoadWnds(
        [
            "Prefabs/WndMain"//主界面
            //同层级的界面可以放数组，一次性加载
        ],
        WndLayers.Base//主界面所在的层级
        )
 
        //等待全部窗体装载完成 
        MD.WndManage.WaitLoadDone(() => {
            console.log("JSLoading 窗体加载完成")
            
            platform.OnUIReady(
                () => {            
                    //初始化游戏逻辑
                    this.InitLogic()

                    //进入首个可操作UI打点
                    AnalyticsMgr.SendEnterFirstUI()

                    //通知内核UI准备就绪
                    BaseEventDispatcher.OnUIReady()
                }
            )

        }
        )
    } 

    InitLogic() {
        try {

            //通知内核首个逻辑界面开始显示
            BaseEventDispatcher.OnFirstMainWndShow()

            //显示主界面
            MD.WndMain.Show()
  
            //初始化作弊界面
            WndDebug.Go()
 
            //高性能定时器开始工作
            UTimeLine.Go()

        } catch (e) {
            console.log(e)
        }
    } 
}
